import { defineStore } from "pinia";
import { 
  postUserList, 
  deleteUserById, 
  newUserInfo, 
  editUserInfo,
  /**下面是封装通用请求 */
  postPageListData,
  deletePageById,
  newPageData,
  editPageData
} from '@/service/main/system/system'


interface IUser {
  id: number
  name: string
  realname: string
  cellphone: number
  enable: number
  departmentId: number
  roleId: number
  createAt: string
  updateAt: string
}

interface ISystemState {
  usersList: IUser[]
  usersTotalCount: number
  pageList: any[]
  pageTotalCount: number
}

const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,
    pageList: [],
    pageTotalCount: 0
  }),
  actions: {
    async postUserListAction(queryInfo: any) {
      const usersListResult = await postUserList(queryInfo)
      // console.log(usersListResult)
      const { totalCount, list } = usersListResult.data
      this.usersList = list
      this.usersTotalCount = totalCount
    },
    async deleteUserByIdAction(id: number) {
      //删除数据的操作
      const deleteUserByIdResult = await deleteUserById(id)
      // console.log(deleteUserByIdResult)

      //重新请求新的数据
      this.postUserListAction({offset: 0, size: 10})
    },
    async newUserInfoAction(userInfo: any) {
      //创建新用户
      const newResult = await newUserInfo(userInfo)
      // console.log(newResult)
      //重新请求新的数据
      this.postUserListAction({offset: 0, size: 10})
    },
    async editUserInfoAction(id: number, userInfo: any){
      //更新用户数据  
      const editUserResult = await editUserInfo(id, userInfo)

      //重新请求新的数据
      this.postUserListAction({offset: 0, size: 10})
    },

    //针对页面的数据增删改查
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo)
      const { totalCount, list } = pageListResult.data
      // console.log(pageListResult)
      this.pageList = list
      this.pageTotalCount = totalCount
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const pageListResult = await deletePageById(pageName, id)
      // console.log(pageListResult)
      this.postPageListAction(pageName, {offset: 0, size: 10})
    },
    async newPageDataAction(pageName: string, pageInfo: any) {
      const newResult = await newPageData(pageName, pageInfo)
      // console.log(newResult)

      this.postPageListAction(pageName, {offset: 0, size: 10})
    },
    async editPageDataAction(pageName: string, id: number, pageInfo: any) {
      const editResult = await editPageData(pageName, id, pageInfo)
      // console.log(editResult)
      this.postPageListAction(pageName, {offset: 0, size: 10})
    }
  }
})

export default useSystemStore